import re

from flask import session
from flask_wtf import FlaskForm, RecaptchaField
from flask_wtf.file import FileAllowed, FileField, FileRequired
from wtforms import PasswordField, StringField, SubmitField
from wtforms.validators import (DataRequired, Email, EqualTo, Length,
                                ValidationError)

# 如何让bootstrap和flask_wtf结合使用

# 定义一个form表单类，继承自FlaskForm
class UserLoginForm(FlaskForm):
    """
    Form for user to create an account
    """
    # email = StringField('Email', validators=[DataRequired(), Email()])
    # password = PasswordField('Password', 
    #                          validators=[
    #                              DataRequired(), 
    #                              EqualTo('confirm', message='Passwords must match')])
    
    username = StringField(label='用户名', 
                           validators=[DataRequired(),Length(3, 20,message='用户名长度3-20')])
    password = PasswordField(label='密码', 
                           validators=[DataRequired(),Length(3, 20,message='密码长度3-20')])
    confirm_pwd = PasswordField(label='确认密码',
                           validators=[DataRequired(),Length(3, 20,message='密码长度3-20'),
                                       EqualTo('password',message='两次密码不一致')
                                       ])
    phone = StringField(label='手机号',
                           validators=[DataRequired(),Length(11,11,message='手机号长度11')])
    
    icon = FileField(label='用户头像',
                           validators=[FileRequired(),
                                       FileAllowed(['jpg','jpeg','png','gif'],message='图片格式错误')])
    recaptcha = StringField(label='验证码',
                           validators=[DataRequired(),Length(4,4,message='验证码长度4')])

    # submit= SubmitField('提交')
    def validate_username(self, data): # 用户名的校验方法
        """
        Check if username is already registered
        """
        # print(self.username.data)
        # data.data 就是表单提交的数据
        if self.username.data[0].isdigit():
            raise ValidationError('用户名不能以数字开头') # 抛出异常，会在前端显示错误信息
    
    def validate_phone(self, data): # 手机号的校验方法
        """
            验证手机号必须是11位数字
        """
        # print(self.phone.data)
        phone = data.data # 获取到表单中手机号码的value
        if not re.search(r'^1[3456789]\d{9}$',phone):
            raise ValidationError('手机号格式不正确') 
        
    def validate_recaptcha(self, data):
        """
            验证验证码是否正确
        """
        input_code = data.data # 获取到表单中输入的验证码
        code = session.get('code') # 得到session中的验证码
        print(f'input_code: {input_code} code: {code}')
        if input_code.lower() != code.lower():
            raise ValidationError('验证码错误') 
        
        
class UserRegisterForm(FlaskForm):
    """
    Form for user to create an account
    """
    username = StringField('username', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), EqualTo('confirm', message='Passwords must match')])
    confirm = PasswordField('Confirm Password')
    submit= SubmitField('注册')